<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/31 0031
 * Time: 上午 11:24
 */

namespace app\api\service;

use app\common\service\Service;
use think\Db;

class Question extends Service
{

    public function getQuestion($where=[],$sort='sort',$asc='asc')
    {
        $arr = [];
        $uid = 0;
        if(!empty($where['uid'])){
            $uid = $where['uid'];
            unset($where['uid']);
        }
		
		$cat = db('question_cat')->field('id,name')->where('is_show',1)->order('sort asc')->select();
		foreach ($cat as $kk => $vv)
		{
			$arr = [];
			$goods = $this->setModel('Question')->where($where)->where('cat_id',$vv['id'])->order($sort,$asc)->select();
			if($goods)
			{
				foreach ($goods as $kk => $vv)
				{
					$arr[$kk]['id'] = $vv['id'];
					$arr[$kk]['name'] = $vv['name'];
					//$arr[$kk]['type'] = $vv['type'];
					//$arr[$kk]['wd_max_point'] = $vv['wd_max_point'];
					//$arr[$kk]['wd_min_point'] = $vv['wd_min_point'];
					//$arr[$kk]['question_content'] = $vv['question_content'];
					$arr[$kk]['title'] = $vv['title'];
					$arr[$kk]['cover'] = str_replace("\\","/",config('app_host').$vv['cover']);
					//$arr[$kk]['small_cover'] = str_replace("\\","/",config('web_url').$vv['small_cover']);
					//$arr[$kk]['tag'] = db('question_tag_log')->alias('g')->where('g.question_id',$vv['id'])->field('think_question_tag.name,think_question_tag.id')->join('think_question_tag','g.tag_id = think_question_tag.id')->order('g.sort asc')->select();
					//$arr[$kk]['fee'] = $vv['fee'];
					$arr[$kk]['cp_num'] = $vv['cp_num'];
					$arr[$kk]['cp_time'] = $vv['cp_time'];
					$arr[$kk]['question_nums'] = Db::name('question_list')->where('question_id',$vv['id'])->where('is_show',1)->count();
					//$arr[$kk]['want_sale'] = $vv['want_sale'];
					//$c_count = Db::name('comment')->where('question_id',$vv['id'])->where('is_show',1)->count();
					//$s_count = Db::name('question_collect_log')->where('question_id',$vv['id'])->count();
					//$s_status = Db::name('question_collect_log')->where('uid',$uid)->where('question_id',$vv['id'])->find();
					//$arr[$kk]['c_count'] = $c_count;
					//$arr[$kk]['s_count'] = $s_count;
					//$arr[$kk]['s_status'] = $s_status?1:0;
				}
			}
			$cat[$kk]['question_list'] = $arr;
		}

        return $this->successResult('获取数据成功！',$cat);
    }
	
    //获取详情
    public function getQuestionDetail($good_id,$uid=0)
    {
        $arr = [];
        $goods = $this->setModel('Question')->find($good_id);
        if(!$goods) return $this->errorResult('不存在');
        if($uid){
            $noWhere = [];
            $question_nos = Db::name('question_log')->where('question_id',$good_id)->where('uid',$uid)
                ->where('status',1)->order('create_time','desc')->group('question_no')->limit(1)->value('question_no');
            if($question_nos)$noWhere = [$question_nos];
            Db::name('question_log')->where('question_id',$good_id)->where('uid',$uid)
                ->where('status',1)->whereNotIn('question_no',$noWhere)->update(['status'=>0,'update_time'=>time()]);
        }

        //$c_count = Db::name('comment')->where('question_id',$good_id)->where('is_show',1)->count();
        $s_count = Db::name('question_collect_log')->where('question_id',$good_id)->count();
        $s_status = Db::name('question_collect_log')->where('uid',$uid)->where('question_id',$good_id)->find();

        $arr['id'] = $good_id;
        $arr['name'] = $goods->name;
        $arr['type'] = $goods->type;
        $arr['title'] = $goods->title;
        $arr['wd_max_point'] = $goods->wd_max_point;
        $arr['wd_min_point'] = $goods->wd_min_point;
        $arr['tag'] = db('question_tag_log')->alias('g')->where('g.question_id',$good_id)->field('think_question_tag.name,think_question_tag.id')->join('think_question_tag','g.tag_id = think_question_tag.id')->order('g.sort asc')->select();
        $arr['question_remark'] = $goods->question_remark;
        //$arr['fee'] = $goods->fee;
        $arr['cp_num'] = $goods->cp_num;
        $arr['is_sex'] = $goods->is_sex;
        $arr['want_sale'] = $goods->want_sale;
        $arr['cover'] = str_replace("\\","/",config('web_url').$goods->cover);
        //$arr['c_count'] = $c_count;
        $arr['s_count'] = $s_count;
        $arr['s_status'] = $s_status?1:0;
        $arr['question_content'] = $goods->question_content;
        //生成一个答题批次
        $arr['question_no'] = get_order_sn('Q');
        return $this->successResult('成功',$arr);
    }
	
	
    public function getQuestionList($where=[],$page=1,$page_size = 15,$sort='sort',$asc='asc')
    {
        $arr = [];
        $goods = $this->setModel('QuestionList')
            ->where($where)->page($page,$page_size)->order($sort,$asc)->select();
        if($goods)
        {
            foreach ($goods as $kk => $vv)
            {
                $arr[$kk]['id'] = $vv['id'];
                $arr[$kk]['title'] = $vv['title'];
                if($vv['type'] == 2) $arr[$kk]['title'] = str_replace("\\","/",config('web_url').$vv['cover']);
                $arr[$kk]['num'] = $vv['sort'];
                $arr[$kk]['remark'] = $vv['question_remark'];
                $arr[$kk]['type'] = $vv['type'];
                $arr[$kk]['answer_type'] = $vv['answer_type'];
                $arr[$kk]['cover'] = str_replace("\\","/",config('web_url').$vv['cover']);
                $arr[$kk]['is_jx'] = $vv['is_jx'];
                $arr[$kk]['jx_content'] = $vv['jx_content'];
                //获取选项
                $option = Db::name('question_option')
                    ->where('question_list_id',$vv['id'])
                    ->where('is_show',1)->field('id,option_id,title,cover,type,is_right,sort')->order('sort')->select();
                foreach ($option as $k1 => $v1){
                    $option[$k1]['option'] = Db::name('option')->where('id',$v1['option_id'])->value('name');
                    if($v1['type'] == 2) $option[$k1]['title'] = str_replace("\\","/",config('web_url').$v1['cover']);
                }
                $arr[$kk]['option'] = $option;

            }
        }
        return $this->successResult('获取数据成功！',$arr);
    }

    public function doQuestion($postData,$uid)
    {
        //{'question_id':1,'question_list_id':1,'option_id':[1,2],'answer':'','question_no':''}
        //$type = $postData['type']; // 1 正常答题 2 继续答题
        if(empty($postData['question_no']))  return $this->errorResult('缺少答题号！');
        if(empty($postData['question_id']))  return $this->errorResult('缺少答题id！');
        if(empty($postData['question_list_id']))  return $this->errorResult('缺少答题题目id！');
        if(empty($postData['option_id']))  return $this->errorResult('缺少答题题目选项！');
        $inData = [
            'uid' => $uid,
            'status' => 1,
            'question_no' => $postData['question_no'],
            'question_id' => $postData['question_id'],
            'question_list_id' => $postData['question_list_id'],
            'answer_content' => $postData['answer'],
            'option_id' => $postData['option_id'],
        ];
        //有效答题记录
//        $log = Db::name('question_log')->where('question_no',$postData['question_no'])->find();
//        if($log){
//            $logs = json_decode($log['logs'],true);
//            if($logs && is_array($logs)){
//                array_push($logs,$postData);
//                $inData['logs'] = json_encode($logs);
//            }
//            $inData['update_time'] = time();
//            Db::name('question_log')->where('question_no',$postData['question_no'])->update($inData);
//        }else{
//            $inData['create_time'] = time();
//            Db::name('question_log')->insert($inData);
//        }

        $inData['create_time'] = time();
        Db::name('question_log')->insert($inData);
        //以前的无效
        return $this->successResult('成功');
    }

    public function finishQuestion($postData,$uid)
    {
        //{'question_id':1,'question_list_id':1,'option_id':[1,2],'answer':'','question_no':''}
        //$type = $postData['type']; // 1 正常答题 2 继续答题
        //以前的无效
        $log1 = Db::name('finish_question')->where('question_no',$postData['question_no'])->find();
        if($log1) return $this->errorResult('已提交！');
        if(empty($postData['question_no']))  return $this->errorResult('缺少答题号！');
        if(empty($postData['question_id']))  return $this->errorResult('缺少答题id！');
        $inData = [
            'uid' => $uid,
            'sex' => $postData['sex']??'',
            'question_no' => $postData['question_no'],
            'question_id' => $postData['question_id'],
            'order_id' => $postData['order_id'],
            'remark' => $postData['remark']??'',
            'update_time' => time(),
            'create_time' => time(),
        ];
        $finish_id = Db::name('finish_question')->insertGetId($inData);
        //有效答题记录
        Db::name('question_log')->where('question_no',$postData['question_no'])->update(['status'=>2,'finish_id'=>$finish_id,'update_time'=>time()]);
        //以前的无效
        Db::name('question_log')->where('uid',$uid)
            ->where('question_id',$postData['question_id'])->where('status',1)->update(['status'=>0,'update_time'=>time()]);

        return $this->successResult('成功',['finish_id'=>$finish_id]);
    }


    //获取分类
    public function getQuestionCat()
    {
        $cat = db('question_cat')->where('is_show',1)->order('sort asc')->select();
        $arr = [];
        if($cat)
        {
            foreach ($cat as $kk => $vv)
            {
                $arr[$kk]['id'] = $vv['id'];
                $arr[$kk]['name'] = $vv['name'];
                $arr[$kk]['icon'] = str_replace("\\","/",config('web_url').$vv['icon_url']);
            }
        }
        return $this->successResult('成功',$arr);
    }
}